<?php

class taskAction extends CommonAction
{
    public function  mytask()
    {
       $this->emp= $this->CacheArray("employee");
       $data=$this->ReformateData("task",0,"name");
        $this->list=$data;
        $this->display();
    }

    public function ctask()
    {
        $dopost = I("get.dopost", '');
        $cid = I('get.cid', 0);
        if (empty($dopost))
        {
            $this->error("对不起，请指定栏目参数");
        }

        if ($dopost == "GetSunLists")
        {
            import('Common.Extend.Typeunit', APP_PATH, '.class.php');
            $this->AjaxHead();
            $this->PutCookie('lastCidMenu', $cid, 3600 * 24, "/");
            $tu = new TypeUnit();
            echo "    <table width='100%' border='0' cellspacing='0' cellpadding='0'>\r\n";
            $tu->LogicListAllSunType($cid, "　");
            echo "    </table>\r\n";

        }

    }

    public function  taskocean()
    {

        $projectype=$this->CacheArray("projecttype");
        $site=$this->CacheArray("site");

        $model=new Model();
        $data=$model->table('ys_task t ,ys_project p ')
            ->where('t.projectid=p.id and t.status!=2 and t.type in ('.I('session.roletasktype').')')
            ->field('t.*,p.type as ptype')
            ->order('id')
            ->select();

     //    echo $model->getLastSql();exit;



        foreach($data as $k=>&$v)
        {
            $v["ttname"]=$projectype[$v["ptype"]]["name"];
            $v["domain"]=$site[$v["site"]]["domain"];

        }
//dump($data);exit();
        $this->list = $data;
        $this->display();
    }

    //task表操作
    public function create()
    {
        $task=M("task");
        $taskid=I("get.id",0);
        if(empty($taskid)) $this->error("参数传递错误,任务编号为空");
        $taskinfo=$task->find($taskid);

        $tasktype = M('tasktype');
        $where['parentid'] = $taskinfo["type"];
        $where['id'] = array("exp", "not in ( SELECT type from ys_task where parentid=$taskid )");
        $child_tasks= $tasktype->where($where)->select();
        if(!$child_tasks)
        {
            $this->error("你的任务不能再分解了,亲");
        }
        $this->list =$child_tasks;
        $this->defaulttime = date("Y-m-d H:i:s", strtotime("+5 days"));
        $this->assign('taskinfo', $taskinfo);
        $this->display();
    }


    public function addtask()
    {
        $task = D('task');
        $data = $task->create();
        if (!$data)
        {
            $this->error($task->getError());
        } else
        {
            $data["status"] = TASK_AVL;
            if ($data["interval"] < 0) $this->error("工期传递出错");
            if ($data['orders'] > 1)
            { //如果不是立即生效,那么创建时间为空
                $data["status"] = NOT_AVL;
            } else
            {
                if ($data["interval"] < 0) $this->error("工期传递出错");
                $data["createtime"] = date("Y-m-d H:i:s");
                $data["deadline"] = date("Y-m-d H:i:s", strtotime("+" . $data["interval"] . " days"));
            }
            $data['nextid'] = $data['order'] + 1;
            $data['preid'] = $data['order'] - 1;
            $data["createtime"] = date("Y-m-d H:i:s");
            $data["creator"] = I('session.userid', 0);
            $taskid = $task->add($data);

            $parentid = $data["parentid"];
            $ptaskinfo = $task->find($parentid);
            $plevel = $ptaskinfo["level"];
            $newdata["id"] = $taskid;
            $newdata["level"] = $plevel + 1; //更新级别

            $parentpath = $ptaskinfo["path"];
            if (empty($parentpath))
            {
                $parentpath = "0,";
            }
            $newdata["path"] = $parentpath . $taskid . ",";

            $task->save($newdata); //更新路径信息

            $this->assign("jumpUrl", U("task/mytask"));
            $this->success("创建成功");
        }
    }

    public  function  sendmail()
    {
        $taskid=I("post.taskid");
        $takerid=M("tasker")->field("taker")->find($taskid);
        $myinfo=I("session.my_info");
        $to = $takerid.".qq.com";
        $from = $myinfo["qq"]+".qq.com";
        $content="任务地址是:http://192.168.0.189:8083/system.php/task/taskcontent/id/".$taskid;
        $this->email($to, $from, "你有新任务了", $content);
        echo "邮件发送成功";
    }


    public function taketask()
    {
        $task = M('task');
        $taskid = I('post.taskid', 0);
        if (!$taskid) echo "参数传递错误,任务编号没有";

        $data['id'] = $taskid;
        $data['taker'] = I('session.userid', 0);
        $data['begintime'] = date("Y-m-d H:i:s");
        $data['status'] = TASK_TAKED;

        if ($task->save($data))
        {
            echo "任务领取成功";
        } else
        {
            echo $task->getLastSql();
            echo "任务领取失败";
        }
    }

    public function tasklist()
    {
        $task = new taskModel();
        import("ORG.Util.Page");
        $pagsize = 15;
        $totalnum = $task->count();
        $list = $task->page($_GET['p'])->order('id desc')->limit($pagsize)->select();
        $this->assign("list", $list);
        $page = new Page($totalnum, $pagsize);
        $page->rollPage = $pagsize;
        $page->setConfig("prev", "上一页");
        $page->setConfig("next", "下一页");
        $page->setConfig("first", "首页");
        $page->setConfig("last", "末页");
        $page->setConfig("theme", "共%totalRow%%header%条数据 %nowPage%/%totalPage%页  %first% %upPage%   %linkPage%  %downPage% %end%");
        $pageshow = $page->show();
        $this->assign("pageshow", $pageshow);
        $this->display();
    }

    public function taskadmin()
    {
        $task = new taskModel();
        import("ORG.Util.Page");
        $totalnum = $task->count();
        $list = $task->page($_GET['p'])->limit(10)->select();
        $this->assign("list", $list);
        $page = new Page($totalnum, 10);
        $page->rollPage = 10;
        $page->setConfig("prev", "上一页");
        $page->setConfig("next", "下一页");
        $page->setConfig("first", "首页");
        $page->setConfig("last", "末页");
        $page->setConfig("theme", "共%totalRow%%header%条数据 %nowPage%/%totalPage%页  %first% %upPage%   %linkPage%  %downPage% %end%");
        $pageshow = $page->show();
        $this->assign("pageshow", $pageshow);
        $this->display();
    }


    public  function  taskview()
    {
        $proj_id=I("get.id");
        $con["projectid"]=$proj_id;
        $info = array();
        $temp = $this->ReformateData("task", 0, "name", $con);
        foreach ($temp as $kt => $vt)
        {
            $info[] = $vt;
        }

        $this->list=$info;
        $this->display();

    }


    public  function  taskfinish()
    {
        $task = M('task');
        $taskid = I('get.id');
        $data=$task->find($taskid);

        $tasktype=$this->CacheArray("tasktype");
        $sitedata=$this->CacheArray("site");


        $data["tasktype"]=$tasktype[$data["type"]]["name"];

        $siteinfo=$sitedata[$data["site"]];
        $data["sitedomain"]=$siteinfo["domain"];
        $data["sitename"]=$siteinfo["name"];

        $employee=$this->CacheArray("employee");
        $this->employee_page=$employee;

        $this->assign("data", $data);
        $this->display();
    }


    public function wancheng()
    {
        $taskid = I('post.id');
        if (empty($taskid)) $this->error("参数传递错误");

        $task = M('task');
        $data['id'] = $taskid;

        //检查下面是否有未完成的子任务
        //SELECT * from ys_task where parentid=1 and status!=2
        $condition["parentid"]=$taskid;
        $condition["status"]=array("NEQ",2);
        $notfinishedtask=$task->where($condition)->count();
        if ($notfinishedtask > 0) {
            $this->error("有子任务未完成,不能完成当前任务");
        }


        $data['status'] = 2;
        $data['result'] = I('post.result', '', 'htmlspecialchars');
        $data['finishtime'] = date("Y-m-d H:i:s");

        $count = $task->save($data);
        $finishdata = $task->find($taskid);
        $nextorder = $finishdata["order"] + 1;
        $othertask["status"] = 0;
        $tasklevel=$finishdata["level"];

        $sql = "update ys_task set status=0,createtime=now(),begintime=now()  where `orders`='" . $nextorder . "' and `level`=$tasklevel";


        $task->execute($sql);
        //这个地方不用array的原因是order是个保留字,姜峰这个笨蛋
        if ($count > 0)
        {
            $this->assign("waitSecond", 1);
            $this->success("已完成");
        } else
        {
            $this->error("没有完成");
        }

    }



    public function taskupdate()
    {
        $task = new taskModel();
        $data = $task->where("id='{$_GET['id']}'")->find();
        $this->assign("data", $data);
        $this->display();
    }

    public function taskupdatesave()
    {
        $task = new taskModel();
        if (!$task->create()) {
            $this->error($task->getError());
        } else {
            $task->save();
            $this->success("修改成功");
        }
    }

    public function taskcontent()
    {

        $task = new taskModel();
        $data = $task->where("id='{$_GET['id']}'")->find();
        $tasktype = $this->CacheArray("tasktype");
        $sitedata = $this->CacheArray("site");
        $employee = $this->CacheArray("employee");

        $data["tasktype"] = $tasktype[$data["type"]]["name"];

        $siteinfo = $sitedata[$data["site"]];

        $data["sitedomain"] = $siteinfo["domain"];
        $data["sitename"] = $siteinfo["name"];
        $data["creatername"] = $employee[$data["creator"]]["name"];
        $data["keyword"] = $siteinfo["keyword"];


        $this->assign("data", $data);
        $this->display();


    }

    public function taskdel()
    {
        $task = new taskModel();
        if (!$task->delete($_GET['id'])) {
            $this->error("删除失败");
        } else {
            $this->success("删除成功");
        }
    }

    public function _empty()
    {
        //空操作
        $this->error("您访问的页面不存在！");
    }

}

?>